<?php
namespace app\admin\controller;
// use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
use think\captcha\Captcha;
use \app\admin\model\Users as m;
class System extends Common
{


    public function index()
    {
        $list=Db::name("config_list")->where("status=1")->select();
        //print_r($list);
        $db=Db::name("config");
        if(Request::instance()->isPost()){
            $data=$_POST;

//            print_r($_POST);
//            die;

            foreach($_POST as $k=>$v){
                $sql="UPDATE tp_config SET `varvalue`='$v' WHERE varname='$k'";
                if($db->execute($sql)){
                }
//                if($k<>'dosubmit') F($k,$v, TEMP_PATH);
            }
            $this->success("修改成功","/admin/system/index");

        }
        $list1=$db->where("typeid=1")->select();
        $list2=$db->where("typeid=2")->select();
        $list3=$db->where("typeid=3")->select();
        $list4=$db->where("typeid=4")->select();
        $list6=$db->where("vartype=2")->select();
        $this->assign("list1",$list1);
        $this->assign("list2",$list2);
        $this->assign("list3",$list3);
        $this->assign("list4",$list4);
        $this->assign("list6",$list6);
        $list6=$db->where("vartype=2")->select();
        $this->assign("list6",$list6);
        $this->assign("list",$list);
        return $this->fetch("/system/index");
    }


    public function lists()
    {
        $list=Db::name("config_list")->order("id asc")->select();
        $this->assign("list",$list);
        return $this->fetch("/system/lists");
    }


    //添加
    public function list_add()
    {
        $id=input('id',0);


        if($id){
            $v=Db::name("config_list")->find($id);

        }else{
            $v['title']="";
            $v['disorder']=0;
            $v['sendtime']=time();
            $v['status']=1;
        }
        $url="/admin/system/lists";




        if(Request::instance()->isPost()){
            $data=$_POST;

            $data['sendtime']=strtotime($_POST['sendtime']);
            if($id){
                if(Db::name("config_list")->where("id=".$id)->update($data)){
                    $this->success("修改成功",$url);
                }else{
                    $this->error("修改失败");
                }
            }else{
                $data['sendtime']=time();
                if(Db::name("config_list")->insert($data)){
                    $this->success("添加成功",$url);
                }else{
                    $this->error("添加失败");
                }
            }
        }
        $this->assign("v",$v);


        return $this->fetch("/system/list_add");
    }


    //审核
    public function list_audit()
    {

        $id=$_POST['id'];
        $zd=$_POST['zd'];
//        print_r($_POST);
        $v=Db::name("config_list")->find($id);
        $data[$zd]=$v[$zd]?0:1;

        if(Db::name("config_list")->where("id=".$id)->update($data)){
//            $this->success("审核成功","/admin/config/list.html");
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
////            $this->error("审核失败");
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }
//
        echo json_encode($return_data);


    }




    public function config()
    {
        $uid=input('uid',0);
        if($uid)$map['uid']=$uid;

        $list=Db::name("config")->where($map)->order("id asc")->select();
        $this->assign("list",$list);
        $this->assign("uid",$uid);
        return $this->fetch("/system/config_list");
    }


    public function map()
    {
        return $this->fetch("/system/map");
    }


    //添加
    public function config_add()
    {
        $id=input('id',0);
//        $id=$_POST['id'];
//        echo $id;
        $uid=input('uid',0);
        if($id){
            $v=Db::name("config")->where("id=".$id)->find();
//            $id=$v['id'];
        }else{
//            $v['id']=Db::name("config")->max("id")+1;
            $v['title']="";
            $v['uid']=$uid;
            $v['disorder']=0;
            $v['sendtime']=time();
            $v['status']=1;
            $v['varinfo']="";
            $v['varvalue']="";
            $v['varname']="";
            $v['vartype']=0;
        }
        $url="/admin/system/config/uid/".$uid;
//        print_r($v);
        if(Request::instance()->isPost()){
            $data=$_POST;

            if($id){
                if(Db::name("config")->where("id=".$id)->update($data)){
                    $this->success("修改成功",$url);
                }else{
                    $this->error("修改失败");
                }
            }else{
                if(Db::name("config")->insert($data)){
                    $this->success("添加成功",$url);
                }else{
                    $this->error("添加失败");
                }
            }
        }
        $this->assign("v",$v);
        $this->assign("uid",$uid);


        return $this->fetch("/system/config_add");
    }

    //审核
    public function config_audit()
    {

        $id=$_POST['id'];
        $v=Db::name("config")->where("id={$id}")->find();
        $data["status"]=$v["status"]?0:1;

        if(Db::name("config")->where("id=".$id)->update($data)){
            $return_data['status'] = 0;
            $return_data['msg'] ="审核成功";
        }else{
////            $this->error("审核失败");
            $return_data['status'] = 1;
            $return_data['msg'] ="审核失败";
        }
//
        echo json_encode($return_data);


    }


}